﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IniFileManager
{
	/// <summary>
	/// Class which represents property of ini file.
	/// </summary>
	sealed public class IniProperty : IComparable<string>, IComparable<IniProperty>, IIniFileToken
	{
		internal static readonly IniProperty EmptyProperty = new IniProperty( "" );
		private string name;
		private List<IniElement> elements;

		/// <summary>
		/// Constructor of IniProperty.
		/// </summary>
		/// <param name="name">Name of property.</param>
		public IniProperty( string name )
		{
			this.name = name;
			this.elements = new List<IniElement>();
		}

		/// <summary>
		/// Gets property name.
		/// </summary>
		public string Name
		{
			get
			{
				return this.name;
			}
		}

		/// <summary>
		/// Gets property elemets.
		/// </summary>
		public List<IniElement> Elements
		{
			get
			{
				return this.elements;
			}
		}

		/// <summary>
		/// Method which add new element to property.
		/// </summary>
		/// <param name="newElement">New element.</param>
		public void AddElement( IniElement newElement )
		{
			this.elements.Add( newElement );
		}

		#region Object Members

		/// <summary>
		/// Method which compares 2 properties.
		/// </summary>
		/// <param name="obj">Compared obj.</param>
		/// <returns>Return true if properties are equal. Return false otherwise.</returns>
		public override bool Equals( object obj )
		{
			if( obj.GetType() != typeof( IniProperty ) )
			{
				return false;
			}

			return this.name.Equals( ( (IniProperty)obj ).Name );
		}

		/// <summary>
		/// Returns hash code of property.
		/// </summary>
		/// <returns>Hash code.</returns>
		public override int GetHashCode()
		{
			return this.name.GetHashCode();
		}

		/// <summary>
		/// Method returns property as string.
		/// </summary>
		/// <returns>Property name.</returns>
		public override string ToString()
		{
			return this.name;
		}

		#endregion

		#region IComparable<string> Members

		/// <summary>
		/// Method provides comaparing of 2 properties.
		/// </summary>
		/// <param name="other">Compared object.</param>
		/// <returns>Returns 0 if section names is same as other. Returns -1 otherwise.</returns>
		public int CompareTo( string other )
		{
			if( this.name == other )
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}

		#endregion

		#region IComparable<IniProperty> Members

		/// <summary>
		/// Method provides comaparing of 2 properties.
		/// </summary>
		/// <param name="other">Compared property.</param>
		/// <returns>Returns 0 if section names is same as other. Returns -1 otherwise.</returns>
		public int CompareTo( IniProperty other )
		{
			if( this.Name == other.Name )
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}

		#endregion

		#region IIniFileToken Members

		/// <summary>
		/// Method which is used to writing self to ini file. 
		/// </summary>
		/// <returns>String which will be written to ini file.</returns>
		public string WriteSelf()
		{
			StringBuilder resultBuildet = new StringBuilder();

			if( this.Name.Length > 0 )
			{
				resultBuildet.Append( this.Name );
				resultBuildet.Append( TextConstants.defaultWhiteSpace );
				resultBuildet.Append( TextConstants.assigmentChar );
				resultBuildet.Append( TextConstants.defaultWhiteSpace );

				for( int i = 0; i < this.elements.Count; i++ )
				{
					resultBuildet.Append( this.elements[i].WriteSelf() );
					if( i < ( this.elements.Count - 1 ) )
					{
						resultBuildet.Append( ", " );
					}
				}
			}

			return resultBuildet.ToString();
		}

		#endregion
	}
}
